package cn.chinasyq.photoquan.photo;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.user.activity.PaymentGameActivity;
import cn.chinasyq.photoquan.user.bean.GameOrder;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.chinasyq.photoquan.util.CameraHead;
import cn.chinasyq.photoquan.util.DialogUtil;
import cn.master.util.utils.RequestCodeUtil;
import cn.master.util.utils.ScreenUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.commons.VolleyHelper;
import cn.master.volley.models.response.handler.ResponseHandler;

/**
 * Created by 一搏 on 2015/12/19.
 */
public class GameParticipateInfoActivity extends Activity implements View.OnClickListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnFailedListener, ResponseHandler.OnUploadingListener {
    private static final String TAG_LOADING = "loading";
    private static final String TAG_GIVE_UP = "give_up";
    private static final String TAG_UPLOAD = "upload";
    String uuid;
    TextView et_title, et_name, et_info;
    Button btn1, btn2;
    private NetworkImageView iv;
    private GameOrder order;
    private TextView et_status;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_participate_info);
        uuid = getIntent().getStringExtra("uuid");
        et_info = (TextView) findViewById(R.id.et_info);
        et_name = (TextView) findViewById(R.id.et_name);
        et_title = (TextView) findViewById(R.id.et_title);
        et_status = (TextView) findViewById(R.id.status);
        btn1 = (Button) findViewById(R.id.button4);
        btn2 = (Button) findViewById(R.id.button5);
        iv = (NetworkImageView) findViewById(R.id.iv);
        iv.setLayoutParams(new LinearLayout.LayoutParams(ScreenUtil.getScreenWidth(this),ScreenUtil.getScreenWidth(this)));
        loading();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                onBackPressed();
                break;
            case R.id.button4:
                payGame();
                break;
            case R.id.button5:
                DialogUtil.showAskDialog(GameParticipateInfoActivity.this, "提示", "是否确认放弃参赛？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        giveUp();
                    }
                }, false);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RequestCodeUtil.getInstance().obtainRequestCode(PaymentGameActivity.class)) {
            loading();
        } else {
            Uri bitmap = null;
            try {
                bitmap = CameraHead.callbackZoom(this, requestCode, resultCode, data, 0, 0);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (bitmap != null) {
                upLoad(bitmap);
            }
        }
    }

    public void upLoad(Uri image) {
        ResponseHandler handler = new ResponseHandler(TAG_UPLOAD);
        handler.setOnSucceedListener(this);
        handler.setOnNeedLoginListener(this);
        handler.setOnFailedListener(this);
        handler.setOnUploadingListener(this);
        UserMode.uploadGamePhoto(new File(BitmapUtil.convertUri2FilePath(this, image)), uuid, UserMode.getCurrentUserInfo(this).getToken(), handler);
        if (dialog == null) {
            dialog = DialogUtil.createProgressDialog(this);
        }
        dialog.dismiss();
    }

    public void loading() {
        ResponseHandler handler = new ResponseHandler(TAG_LOADING);
        handler.setOnSucceedListener(this);
        handler.setOnNeedLoginListener(this);
        handler.setOnFailedListener(this);
        handler.setOnUploadingListener(this);
        UserMode.obtainGameOrderInfo(UserMode.getCurrentUserInfo(this).getToken(), uuid, handler);
    }

    public void giveUp() {
        ResponseHandler handler = new ResponseHandler(TAG_GIVE_UP);
        handler.setOnSucceedListener(this);
        handler.setOnNeedLoginListener(this);
        handler.setOnFailedListener(this);
        handler.setOnUploadingListener(this);
        UserMode.giveUpGame(UserMode.getCurrentUserInfo(this).getToken(), uuid, handler);
    }

    private void payGame() {
        Intent intent = new Intent(this, PaymentGameActivity.class);
        intent.putExtra(GameOrder.class.getSimpleName(), order);
        startActivityForResult(intent, RequestCodeUtil.getInstance().obtainRequestCode(PaymentGameActivity.class));
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (data instanceof GameOrder) {
            order = (GameOrder) data;
            order.getUuid();
            et_info.setText(order.getDescription());
            et_name.setText(order.getUser_name());
            et_title.setText(order.getTitle());
            iv.setImageUrl(order.getImage(), VolleyHelper.getImageLoader());
            et_status.setText(order.obtainStatic());
            if ("main_create".equals(order.getState().getVal())){
                btn1.setText("待付款");
                btn1.setBackgroundResource(R.drawable.btn_c8_c10);
                btn1.setTextColor(getResources().getColor(R.color.C8));
                btn2.setVisibility(View.VISIBLE);
                btn1.setVisibility(View.VISIBLE);
            }else {
                btn1.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
            }
            if (dialog != null) {
                dialog.dismiss();
            }
        } else {
            loading();
        }
    }

    @Override
    public void onNeedLogin(String tag) {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
    }

    @Override
    public void onUploading(String tag, int progress) {

    }
}
